﻿var shapeNames = [];
var shapeName = null;
//初始化设置图层目录树
function LayerTreeWidget(id, LayerInfo) {
    // 属性
    this.id = id;
    this.operid = id + "-operatebar";
    this.LayerInfo = LayerInfo;
    this.fixed = true;
    //方法
    if (typeof this.CreateWidget != "function") {
        LayerTreeWidget.prototype.CreateWidget = function() {
            createLayerTreeWidget(this.id, this.LayerInfo);
            createOperateWidget(this.operid, this.LayerInfo);
        };
    }
    //组件创建
    //#region 初始化创建目录树布局，包括主窗体、标题栏、图层组及图层内容
    function createLayerTreeWidget(id, LayerInfo) {
        //创建主窗体
        var lyWidget = createWidgetMainForm(id);
        //窗体固定初始化事件注册
        initialWidgetDragEvent(lyWidget.parentElement.parentElement);
        //创建主窗体中的标题栏-标题名称、关闭操作、固定操作初始化
        createWidgetTitleBar(lyWidget, id);
        //创建主窗体中的内容栏-目录树
        createWidgetContentBar(lyWidget, LayerInfo);
        //窗体固定设置
        //layerControlFixed(true);
        //创建主窗体div
        function createWidgetMainForm(id) {
            var lyWidget = document.createElement('div');
            lyWidget.id = id;
            document.body.appendChild(lyWidget);

            var lyScrollBox = document.createElement('div');
            lyScrollBox.className = "LayerTree-main-form-scrollbox";
            lyWidget.appendChild(lyScrollBox);

            var analysisPanel = document.createElement("div"); //
            analysisPanel.id = "LayerTree-main-form-panel";
            analysisPanel.className = "LayerTree-main-form-panel";
            //analysisPanel.style = "margin-top:0px;";
            lyScrollBox.appendChild(analysisPanel);
            return analysisPanel;
        }
        //创建主窗体下标题栏div
        //#region createWidgetTitleBar(lyWidget, id)
        function createWidgetTitleBar(lyWidget, id) {
            //目录树头部
            var layerTree_main_form_header = document.createElement("div"); //
            layerTree_main_form_header.className = "LayerTree-main-form-header";
            layerTree_main_form_header.setAttribute("style", "text-align:left;");
            lyWidget.appendChild(layerTree_main_form_header);

            //设置图标
            var layerTree_mainform_header_icon = document.createElement("i");
            layerTree_mainform_header_icon.className = "headericon";
            layerTree_main_form_header.appendChild(layerTree_mainform_header_icon);

            //设置标题名称
            var layerTree_mainform_header_text = document.createElement("i");
            layerTree_mainform_header_text.className = "title";
            layerTree_mainform_header_text.innerText = "数据目录";
            layerTree_main_form_header.appendChild(layerTree_mainform_header_text);


            //设置固定图标
            var layerTree_mainform_header_fixed = document.createElement("i");
            layerTree_mainform_header_fixed.className = "LayerTree-main-form-fixed";
            layerTree_mainform_header_fixed.id = "LayerTree-main-form-fixed";
            layerTree_mainform_header_fixed.title = "固定";
            layerTree_mainform_header_fixed.onclick = function() {
                //layerControlWindowFixed();
            }
            layerTree_main_form_header.appendChild(layerTree_mainform_header_fixed);
            //设置关闭图标
            var layerTree_mainform_header_btn = document.createElement("i");
            layerTree_mainform_header_btn.tite = "关闭";
            layerTree_mainform_header_btn.className = "LayerTree-main-form-closebutton";
            layerTree_mainform_header_btn.onclick = function clk() { return CloseLayerDlg(id); }
            layerTree_main_form_header.appendChild(layerTree_mainform_header_btn);
        }

        //#endregion
        //创建主窗体下内容栏div-目录树
        //#region createWidgetContentBar(lyWidget, layerInfo) 
        function createWidgetContentBar(lyWidget, LayerInfo) {
            //创建内容栏
            var LayerTree_main_form_body = document.createElement("div"); //
            LayerTree_main_form_body.className = "LayerTree-main-form-body";
            LayerTree_main_form_body.id = "LayerTree-main-form-body";
            lyWidget.appendChild(LayerTree_main_form_body);
            //目录树主体内容
            var LayerTree_main_form_Contentbar = document.createElement("div"); //
            LayerTree_main_form_Contentbar.className = "LayerTree-main-form-content";
            LayerTree_main_form_Contentbar.id = "LayerTree-main-form-content";
            LayerTree_main_form_body.appendChild(LayerTree_main_form_Contentbar);
            //show3DLayers (LayerInfo)
            var itemIndex = {}; //索引信息
            itemIndex["root"] = { count: 0, index: 0 };
            for (var i = 0; i < layerInfo.length; i++) { //遍历图层目录树记录
                var key = layerInfo[i];
                var strClassName = ""; //通用样式
                var strName = ""; //名称
                var isLayer = false; //是否为图层
                var treeNode = {
                    level: 0,
                    mainDiv: { //总体DIV容器
                        class: "",
                        pname: ""
                    },
                    contentDiv: { //图标及提示信息
                        class: "", //div
                        arrowClass: "", //i
                        iconClass: "", //i
                        checkClass: "", //i
                        textClass: "", //span
                        text: ""
                    }
                };
                treeNode = GetLayerTreeNode(key); //节点信息

                if (itemIndex.hasOwnProperty(treeNode.mainDiv.pname)) {
                    itemIndex[treeNode.mainDiv.pname].count++;
                    itemIndex[treeNode.contentDiv.text] = {};
                    itemIndex[treeNode.contentDiv.text].count = 0;
                }
                if (treeNode.level == 0)
                    itemIndex[treeNode.contentDiv.text].class = treeNode.level + " " + treeNode.mainDiv.pname + itemIndex[treeNode.mainDiv.pname].count;
                else
                    itemIndex[treeNode.contentDiv.text].class = itemIndex[treeNode.mainDiv.pname].class + " " + treeNode.mainDiv.pname + itemIndex[treeNode.mainDiv.pname].count;

                itemIndex[treeNode.contentDiv.text].pname = treeNode.mainDiv.pname;
                //设置内容
                var LayerTree_main_form_Contentbar_item = document.createElement("div");
                LayerTree_main_form_Contentbar_item.className = treeNode.mainDiv.class + " " + itemIndex[treeNode.contentDiv.text].class; // treeNode.mainDiv.class;
                LayerTree_main_form_Contentbar_item.id = LayerTree_main_form_Contentbar_item.className.toString().replace(/\s+/g, "-");
                LayerTree_main_form_Contentbar_item.setAttribute("pname", treeNode.mainDiv.pname);
                LayerTree_main_form_Contentbar.appendChild(LayerTree_main_form_Contentbar_item);
                //设置提示信息
                var LayerTree_main_form_Contentbar_title = document.createElement("div");
                LayerTree_main_form_Contentbar_title.className = treeNode.contentDiv.class;
                LayerTree_main_form_Contentbar_item.appendChild(LayerTree_main_form_Contentbar_title);
                var ArrowOffset = "margin-left:" + (10 + treeNode.level * 10) + "px";
                var itemOffset = "margin-left:" + (40 + treeNode.level * 10) + "px";
                if (treeNode.isCatalog) {
                    //设置展开图标

                    var LayerTree_main_form_Contentbar_title_arrow = document.createElement("i");
                    LayerTree_main_form_Contentbar_title_arrow.className = treeNode.contentDiv.arrowClass; // "fa fa-caret-down ";
                    LayerTree_main_form_Contentbar_title_arrow.setAttribute("style", ArrowOffset);
                    LayerTree_main_form_Contentbar_title.appendChild(LayerTree_main_form_Contentbar_title_arrow);
                }

                var LayerTree_main_form_Contentbar_title_Check = document.createElement("i");
                LayerTree_main_form_Contentbar_title_Check.className = treeNode.contentDiv.checkClass;
                if (!treeNode.isCatalog)
                    LayerTree_main_form_Contentbar_title_Check.setAttribute("style", itemOffset);
                LayerTree_main_form_Contentbar_title_Check.onclick = function() {
                    setLayerLoad(this, layerInfo);
                }
                LayerTree_main_form_Contentbar_title.appendChild(LayerTree_main_form_Contentbar_title_Check);
                //内容
                var LayerTree_main_form_Contentbar_title_Text = document.createElement("span");
                LayerTree_main_form_Contentbar_title_Text.innerText = treeNode.contentDiv.alias; //layerGroup.alias;
                LayerTree_main_form_Contentbar_title_Text.title = treeNode.contentDiv.alias;
                LayerTree_main_form_Contentbar_title_Text.setAttribute("lyname", treeNode.contentDiv.text);
                LayerTree_main_form_Contentbar_title_Text.setAttribute("guid", treeNode.contentDiv.guid);
                if (treeNode.isCatalog)
                    LayerTree_main_form_Contentbar_title_Text.className = "LayerTree-main-form-item-title-span-catalog";
                else
                    LayerTree_main_form_Contentbar_title_Text.className = "LayerTree-main-form-item-title-span-child";
                LayerTree_main_form_Contentbar_title.appendChild(LayerTree_main_form_Contentbar_title_Text);
            }
        }

        //#endregion
        //控制窗体显示
        //#region showLayerTreeList(id)
        function showLayerTreeList(id) {
            var divLayer = document.getElementById(id);
            if (divLayer.style.display == "none") {
                divLayer.style.display = '';
                layerControlWindowFixed(true);
            } else {
                divLayer.style.display = "none";
            }
            return false;
        }

        //#endregion
        //初始化窗体
        //#region initialWidgetDragEvent(lyWidget) 
        function initialWidgetDragEvent(lyWidget) {
            var isMouseOnLayerControl = false;
            //lyWidget.className = 'LayerTree-main-form';
            lyWidget.addEventListener("mouseover", function() { isMouseOnLayerControl = true; });
            lyWidget.addEventListener("mouseout", function() { isMouseOnLayerControl = false; });
            lyWidget.addEventListener("mouseleave", function() {
                setTimeout(function() {
                    if (!layerControlWindowIsFixed && !isMouseOnLayerControl) {
                        showLayerTreeList(lyWidget.id);
                    }
                }, 1000);
            }, false);
        }

        //#endregion

        //固定图层列表取消收回事件；
        //#region layerControlWindowFixed(reset)
        var layerControlWindowIsFixed = false;

        function layerControlWindowFixed(reset) {
            if (reset) {
                layerControlWindowIsFixed = false;
                $("#LayerTree-main-form-fixed").removeClass();
                $("#LayerTree-main-form-fixed").addClass("LayerTree-main-form-fixed");
                $("#LayerTree-main-form-fixed").attr("title", "固定");
            } else {
                if (layerControlWindowIsFixed) {
                    $("#LayerTree-main-form-fixed").removeClass();
                    $("#LayerTree-main-form-fixed").addClass("LayerTree-main-form-fixed");
                    $("#LayerTree-main-form-fixed").attr("title", "固定");
                } else {
                    $("#LayerTree-main-form-fixed").removeClass();
                    $("#LayerTree-main-form-fixed").addClass("LayerTree-main-form-fixed-lock");
                    $("#LayerTree-main-form-fixed").attr("title", "取消固定");
                }
                //layerControlFixed(layerControlWindowIsFixed);
                layerControlWindowIsFixed = !layerControlWindowIsFixed;

            }
        }

        //#endregion
        //定位设置
        //#region layerControlFixed(isfixed)
        function layerControlFixed(isfixed) {
            if (isfixed) {
                $("#LayerTree-main-form-body").css("height", "100%");
                $("#LayerTree-main-form-content").css("height", "100%");
                $("#LayerTree-main-form-content").css("max-height", "100%");
                this.fixed = false;
            } else {
                $("#LayerTree-main-form-body").css("height", "100%");
                $("#LayerTree-main-form-content").css("height", "100%");
                $("#LayerTree-main-form-content").css("max-height", "100%");
                this.fixed = true;
            }
        }

        //#endregion
        //关闭目录树
        function CloseLayerDlg(id) {
            this.fixed = true;
            layerControlWindowIsFixed = true;
            if (document.getElementById(id).style.display != "none") {
                $("#" + id).hide();
            }
        }
    }

    //#endregion

    //#region 初始化创建图层操作栏，包括主窗体、标题栏、图层操作内容
    function createOperateWidget(id, LayerInfo) {
        var lyOperateBar = document.getElementById(id);
        if (!lyOperateBar) {
            var lyOperBar = new LayerOperateBar(id, LayerInfo, "");
            lyOperBar.CreateWidget();
            $("#" + id).hide();
        } else {
            if (lyOperateBar.style.display == "")
                lyOperateBar.style.display = 'none';
        }
    }


    //#endregion

    //数据操作
    //#region 数据操作设置，包括数据目录树展开、收缩、勾选，图层
    //目录树伸缩
    $(document).on("click", ".LayerTree-main-form-item .arrow", function() {
        var pClassName = $(this).parent().parent()[0].className;
        var pClass = ("." + pClassName).replace(/\s+/g, ".");
        if (pClassName.indexOf("active") == -1) {
            //设置子级隐藏
            $(pClass).hide();
            //设置当前节点显示
            $(pClass)[0].style.display = '';
            //更改当前节点伸缩样式
            $(this).addClass("active");
            //更改样式标识
            $(pClass).addClass("active");

        } else {
            //设置子级显示
            $(pClass).show();
            //更改当前节点伸缩样式
            $(this).removeClass("active");
            //更改样式标识
            $(pClass).removeClass("active");
            //更改当前节点伸缩样式
            var pArrow = $(".arrow.active");
            pClass = pClass.replace(".active", "");
            for (i = 0; i < pArrow.length; i++) {
                var itemArrow = pArrow[i];
                var itemPClass = "." + itemArrow.parentElement.parentElement.className.replace(/\s+/g, ".");
                if (itemPClass.indexOf(pClass) > -1) {

                    itemArrow.className = itemArrow.className.replace("active", "");
                }
            }
        }

    });
    //#endregion



    //#region 图层加载、取消加载处理
    function setLayerLoad(myCheck, LayerInfo) {
        var layerTreeBar = $(myCheck).parent().parent();
        var pClassName = $(myCheck).parent().parent()[0].className;
        var pClass = ("." + pClassName).replace(/\s+/g, ".");
        var items = $(pClass);
        var pitem = $("#" + items[items.length - 1].id + " i.checkicon");
        var lywidgetid = $(myCheck).parent().parent().parent().parent().parent().parent().parent()[0].id;
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            var lyname = $("#" + item.id).find("span")[0].getAttribute("lyname");
            var isVisible = false;
            var guid = $("#" + item.id).find("span")[0].getAttribute("guid");
            if (pitem.hasClass("active")) {
                $("#" + item.id + " i.checkicon").removeClass("active");
                $("#" + item.id).find("span").removeClass("active");
                //图层取消加载
                //刷新图层操作widget内容
                var layerOperateList = $("#" + lywidgetid + "-operatebar-ul-li-" + lyname);
                layerOperateList.removeClass("active");

            } else {
                //加载图层
                $("#" + item.id + " i.checkicon").addClass("active");
                $("#" + item.id).find("span").addClass("active");
                //图层加载
                var layerOperateList = $("#" + lywidgetid + "-operatebar-ul-li-" + lyname);
                layerOperateList.addClass("active");
                isVisible = true;

            }
            //地图图层加载/取消加载
            if (guid && guid != "") {
                var thisLayerInfo = m_LoadLayerInfo[guid];
                thisLayerInfo.VISIBLE = isVisible;

                var layerHelp = new LayerHelper(thisLayerInfo);
                var thisLayer = layerHelp.GetLayer();
                if (layerHelp.isload) {
                    if (thisLayer) {
                        thisLayer.setVisible(isVisible);
                        if (thisLayer.type == "TILE" && isVisible == false) {
                            var name = thisLayer.values_.source.values_.name;
                            if (shapeNames.indexOf(name) > -1) {
                                shapeNames.splice(shapeNames.indexOf(name), 1);
                            }
                        } else if (thisLayer.type == "TILE" && isVisible == true) {
                            var name = thisLayer.values_.source.values_.name;
                            if (shapeNames.indexOf(name) < 0) {
                                shapeNames.push(name);
                            }
                        }
                    }
                } else {
                    //地形数据加载
                    if (layerHelp.LayerInfo.SERVICETYPE == "terrain") {
                        if (map3d) {
                            var mySecen = map3d.getCesiumScene();
                            if (thisLayer != null)
                                mySecen.terrainProvider = thisLayer;
                        }
                    }
                    //倾斜数据加载
                    else if (layerHelp.LayerInfo.SERVICETYPE == "oblique") {
                        continue;
                    }
                    //
                    else {
                        shapeNames.push(layerHelp.LayerInfo.NAME);
                        //map.addLayer(thisLayer);                       
                    }

                }
            }
        }
    }

    //#endregion
}